﻿using System;
using System.Threading.Tasks;
using MassTransit;
using MassTransit.Logging.Tracing;
using RabbitMQ.Client;
using System.Text;
namespace Athena.Masstransit.Test
{
    class Program
    {
        static void MasstransitTest()
        {
            var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
            {
                var host = sbc.Host(new Uri("rabbitmq://192.168.1.48/qrcode_ha"), h =>
                {
                    h.Username("luwei");
                    h.Password("123123");
                });
                sbc.ExchangeType = "direct";

            });

            bus.Start();

            Task.Factory.StartNew(() =>
            {
                return bus.Publish(new Test() { test = "123" }, (context) =>
                {
                    context.DestinationAddress = new Uri("rabbitmq://192.168.1.48/qrcode_ha/itps_log_request");
                    context.SetRoutingKey("itps_log_request");
                });
            }).GetAwaiter().GetResult();

            bus.Stop();
            Console.ReadKey();
        }

        static void RabbitmqTest()
        {
            var factory = new ConnectionFactory()
            {
                HostName = "192.168.1.48",
                UserName = "luwei",
                Password = "123123",
                VirtualHost = "qrcode_ha"

            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                var response = channel.QueueDeclarePassive("itps_log_request");

                Console.WriteLine(response.MessageCount);
                Console.WriteLine(response.QueueName);

                string message = "Hello World!";
                var body = Encoding.UTF8.GetBytes(message);
                channel.BasicPublish(exchange: "itps_log_request",
                                     routingKey: "itps_log_request",
                                     basicProperties: null,
                                     body: body);
                Console.WriteLine(" [x] Sent {0}", message);
            }

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }

        static void Main(string[] args)
        {
            RabbitmqTest();
        }
    }
}
